# -*- coding: utf-8 -*-
# This code written by ChanGOGOGO
""" """
import time
import pytest
from utils import helpers
from playwright.sync_api import sync_playwright
from operate.operate import (
    Login,
    HomePageOperate,
    DialogDesign,
    CalloutTask,
    CallinTask,
)


def pytest_addoption(parser):
    # 从pytest运行命令的--env参数中获取到配置文件名
    parser.addoption("--env", action="store", default="dev", help="配置文件名")


@pytest.fixture(scope="session")
def env_config(request):
    """根据pytest运行命令中--env参数的传值读取对应的配置文件信息"""
    env = request.config.getoption("--env")
    config_name = f"{env}.yaml"
    env_config = helpers.read_yaml(config_name)
    return env_config


@pytest.fixture(scope="session")
def public_config():
    """读取公共配置文件信息"""
    config = helpers.read_yaml("public.yaml")
    return config


@pytest.fixture(scope="session")
def playwright_fixture():
    p = sync_playwright().start()
    yield p
    p.stop()


@pytest.fixture(scope="session")
def browser_fixture(public_config, playwright_fixture):
    headless = public_config.get("headless")
    browser = playwright_fixture.chromium.launch(
        headless=headless, channel="chrome", args=["--start-maximized"]
    )
    # browser = playwright_fixture.chromium.launch(headless=headless, channel='msedge', args=['--start-maximized'])
    yield browser
    browser.close()


@pytest.fixture(scope="session")
def context_fixture(browser_fixture):
    context = browser_fixture.new_context(
        no_viewport=True
    )  # 配合no_viewport为True才能实现浏览器最大化
    yield context
    context.close()


@pytest.fixture(scope="session")
def page_fixture(context_fixture):
    page = context_fixture.new_page()
    yield page
    page.close()


@pytest.fixture(scope="session")
def login(env_config, context_fixture, page_fixture):
    """登录callplus系统后选择业务"""
    aiforce_url = env_config.get("aiforce_url")
    page_fixture.goto(aiforce_url)
    login = Login(page_fixture)
    username = env_config.get("aiforce_id")
    password = env_config.get("aiforce_password")
    login.login(username, password)
    business_name = env_config.get("business_name")
    login.choose_business(business_name)
    with context_fixture.expect_page() as new_page_info:
        new_page = new_page_info.value
    yield new_page
    new_page.close()


@pytest.fixture(scope="session")
def home_page_operate(login):
    """主页相关操作"""
    home_page_operate = HomePageOperate(login)
    return home_page_operate


@pytest.fixture(scope="session")
def dialog_design(login):
    """对话设计相关操作"""
    dialog_design = DialogDesign(login)
    return dialog_design


@pytest.fixture(scope="session", autouse=True)
def get_now_time():
    now_time = time.strftime("%Y%m%d%H%M%S")
    return now_time


@pytest.fixture(scope="session")
def goto_experiment_group_page(home_page_operate, dialog_design):
    home_page_operate.click_left_menu("对话设计")
    dialog_design.scene_tab_switch("实验组")


@pytest.fixture(scope="session")
def create_experiment_group(
    env_config, goto_experiment_group_page, dialog_design, get_now_time
):
    """新建实验组"""
    experiment_group_name = f"UI_实验组-{get_now_time}"
    dialog_design.create_experiment_group(experiment_group_name)
    dialog_design.wait(500)
    return experiment_group_name


@pytest.fixture(scope="session")
def create_child_scene(get_now_time, dialog_design, create_experiment_group):
    """新建实验组子场景"""
    experiment_group_name = create_experiment_group
    child_scene_name = f"{experiment_group_name}-子场景"
    dialog_design.create_child_scene(experiment_group_name, child_scene_name)
    return child_scene_name


@pytest.fixture(scope="session")
def callout_task(login):
    """呼出任务操作对象"""
    callout_task = CalloutTask(login)
    return callout_task


@pytest.fixture(scope="session")
def callin_task(login):
    """呼入任务操作对象"""
    callin_task = CallinTask(login)
    return callin_task
